\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[UKenglish]{babel}
\usepackage{%
xcolor,%
framed,%
xspace,%
mdframed,%
url%
}

\definecolor{light-gray}{gray}{0.90}
\definecolor{shadecolor}{rgb}{1,0.9,0.7}

\title{Analysing termite favouritism in annular arenas\footnote{This is part of the paper: Paiva \textit{et al.}(in review) Scale-free movement patterns in termites emerge from social interactions and preferential attachments. \textit{submitted to PNAS}. }}



\begin{document}
\SweaveOpts{prefix.string=graphs/fig}
\SweaveOpts{concordance=TRUE}
\maketitle
\begin{description}
\item[Compiled data and ran the analysis:]Og DeSouza \url{<og.souza@ufv.br>}
\item[Tracking algorithm:]\ 
\begin{description}
\item[Available at:]\url{https://github.com/dmrib/trackingtermites} 
\item[Wrote the final code:] Danilo Ribeiro
\item[Wrote the first working code:] Sidiney Alves
\item[Supervised coding:] Og DeSouza
\end{description}
\end{description}
\section{Overall rationale}
%\url{https://github.com/dmrib/trackingtermites}

\begin{itemize}
\item Here we inspect whether termites walking in annular arenas would preferentially contact some of their nestmates over others along a \textit{c.a.} 30 min period.
\item our tracking algorithm (available at GitHub \url{https://github.com/dmrib/trackingtermites}) was able to (i) identify each one of the termite individuals in the arena, (ii) track their positions along the whole footage, and (iii) tally the number of time-steps each individual termite acted as a target of a one-to-one interaction. That is to say, when a given termite walked towards another and met it, this second termite was recorded as a ``target". We then recorded the duration of this encounter in terms of time-steps. Then we analysed the proportion of time-steps (relative to the duration of the whole footage) \textit{each} termite acted as a target. In the absence of favouritism, the proportion of time-steps each member of the group acted as a target would not differ among group members (that is our null hypothesis). That is, for a given footage duration, target time-steps would be even distributed among all members of the group. On the other hand, if *some* of the termites in the group would be "preferred targets" over others, they would spend more time-steps acting as targets than other members of the group. In doing so, for a given footage duration, target time-steps would be unevenly distributed among all members of the group. 

\item To do so, we filmed and tracked \underline{each individual} termite in the confined group, tallying the number of time-steps this focal termite spent contacting a given target termite along the whole footage. 
\item This has produced a dataset such as:
\begin{verbatim}
Tallyied time-steps for a group of 3 individuals.
--------------------------------------------------
  trajectory focalTermite targetTermite steps
1  traj00006    termite01     termite02   640
2  traj00006    termite01     termite03   676
3  traj00006    termite02     termite01   635
4  traj00006    termite02     termite03   500
5  traj00006    termite03     termite01   702
6  traj00006    termite03     termite02   502
--------------------------------------------------
\end{verbatim}
\item then, for each individual we averaged number of time-steps it acted as a \underline{target} of an interaction along the whole footage, producing a result such as:
\begin{verbatim}
Averaged time-steps a termite spent as a target.
------------------------------------------------
  targetTermite steps
1     termite01 668.5
2     termite02 571.0
3     termite03 588.0
------------------------------------------------

\end{verbatim}
\item then we ran a Chi-square test to verify whether the proportion of time-steps spent as a target would vary among termite individuals. That is to say, we inspected whether these proportions would depart from an uniform distribution and, if so, to point to the existence of `favourite targets'.
\item in case the Chi-square test did point to significant differences, we counted the number of `favourites' within that given group. These `favourites' were defined as as being the termites whose average number of time-steps spent as a target was larger than the global average of targeting time-steps in the whole footage.
\item The number of preferred target-termites in each arena (y-var) was then regressed against the number of termites therein confined (x-var), in order to inspect how preferential attachments would correlate with group size. Analysis consisted in contrasting the model thereby obtained with a model with zero intercept and slope $=$ 1, that is, a model in which favouritism was absent. Modelling was performed in R, under Generalised Linear Modelling and normal error distribution, followed by residual analysis. Contrasts were made using Akaike Information Criterion (AIC).

\end{itemize}
% We inspected the frequency of interindividual contacts between termites confined together in an annular arena, to verify whether they would favour a given conspecific over the other members of the group.
% 
% To to so, we filmed the termite group over, in average, 34m12s (min $=$ 20m52s; max $=$ 53m12s). We then tracked each individual in the group, tallying the number of time-steps it spent contacting each one of the others. A Python algorithm based on OpenCV was customised to track the termite identities and respective time-steps a given focal termite spent contacting a given target termite. This was done for ALL termites in the group.
% 
% The analysis proceeded inspecting whether the total amount of time-steps a given \textit{target termite} was 
% 
% 
% proportion of time-steps a given focal-target pair of termites spent together would divert from an equal share




\section{Loading and preparing data}
<<>>=
setwd("./")
dt<-read.table("../dat/newdata.dat",sep=",") # termite1 changed to termite01, etc
names(dt)[1] <- "trajectory"
names(dt)[2]<- "focalTermite"
names(dt)[3]<- "targetTermite"
names(dt)[4]<- "steps"
summary(dt)
@
\newpage
\section{Preferences per trajectory}
Preparing functions:

<<>>=
interact <- function(traj){ # interactions with target termite
  ss <- subset(dt,trajectory==traj)
  ss <- droplevels(ss)
  ##
  ## the total number of steps here considers readings
  ## from termite01 > termite02 as well as termite02 > termite01.
  ## We should make it as the mean of both cases.
  totSteps <- sum(ss$steps)
  interactions <- aggregate(ss$steps,list(ss$targetTermite),mean)
  names(interactions)[1] <- "targetTermite"
  names(interactions)[2] <- "steps"
  return(interactions)
}

pl<-function(interactions)
  {pval <- round(chisq.test(interactions[2])$p.value,4)
  expected <- chisq.test(interactions[2])$expected
  barplot(interactions$steps,
        names.arg = interactions$targetTermite,
        las=2,
        main=paste(traj, " p = ",pval ),
        ylab="time-steps",
        #xlab="termite ID",
        ylim=c(0,1.1*max(interactions[2]))
        )
abline(h=expected[1],col="red")
}

summtraj <- function(resultChi){
  expected <- resultChi$expected
  observed <- resultChi$observed
  favourites <- sum(observed>expected)
  total <- length(resultChi$observed)
  totSteps <- sum(resultChi$observed)
  chi <- resultChi$statistic
  pval <- resultChi$p.value
  return(list(total = total, 
              favourites = favourites, 
              totSteps = totSteps,
              chisquare = chi[[1]], 
              pvalue = pval))
}

@
\newpage
\section{Trajectory 00006}
<<traj00006,include=TRUE, fig=TRUE>>=
traj <- "traj00006"
ii <- interact(traj)
ii
resultChi <- chisq.test(ii[2])
resultTraj00006 <-summtraj(resultChi)
resultTraj00006
pl(ii)
svg(filename = "graphs/fig-traj00006.svg")
pl(ii)
dev.off()
@

\section{Trajectory 00007}
<<traj00007,include=TRUE, fig=TRUE>>=
traj <- "traj00007"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00007 <-summtraj(resultChi)
resultTraj00007
pl(ii)
svg(filename = "graphs/fig-traj00007.svg")
pl(ii)
dev.off()
@


\section{Trajectory 00012}
<<traj00012,include=TRUE, fig=TRUE>>=
traj <- "traj00012"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00012 <-summtraj(resultChi)
resultTraj00012
pl(ii)
svg(filename = "graphs/fig-traj00012.svg")
pl(ii)
dev.off()
@

\section{Trajectory 00013}
<<traj00013,include=TRUE, fig=TRUE>>=
traj <- "traj00013"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00013 <-summtraj(resultChi)
resultTraj00013
pl(ii)
svg(filename = "graphs/fig-traj00013.svg")
pl(ii)
dev.off()
@

\section{Trajectory 00095}
<<traj00095,include=TRUE, fig=TRUE>>=
traj <- "traj00095"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00095 <-summtraj(resultChi)
resultTraj00095
pl(ii)
svg(filename = "graphs/fig-traj00095.svg")
pl(ii)
dev.off()
@

\section{Trajectory 00096}
<<traj00096,include=TRUE, fig=TRUE>>=
traj <- "traj00096"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00096 <-summtraj(resultChi)
resultTraj00096
pl(ii)
svg(filename = "graphs/fig-traj00096.svg")
pl(ii)
dev.off()
@

\section{Trajectory 00100}
<<traj00100,include=TRUE, fig=TRUE>>=
traj <- "traj00100"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00100 <-summtraj(resultChi)
resultTraj00100
pl(ii)
svg(filename = "graphs/fig-traj00100.svg")
pl(ii)
dev.off()
@

\section{Trajectory 00130}
<<traj00130,include=TRUE, fig=TRUE>>=
traj <- "traj00130"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00130 <-summtraj(resultChi)
resultTraj00130
pl(ii)
svg(filename = "graphs/fig-traj00130.svg")
pl(ii)
dev.off()
@

\section{Trajectory 00143}
<<traj00143,include=TRUE, fig=TRUE>>=
traj <- "traj00143"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00143 <-summtraj(resultChi)
resultTraj00143
pl(ii)
svg(filename = "graphs/fig-traj00143.svg")
pl(ii)
dev.off()
@

\section{Trajectory 00144}
<<traj00144,include=TRUE, fig=TRUE>>=
traj <- "traj00144"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00144 <-summtraj(resultChi)
resultTraj00144
pl(ii)
svg(filename = "graphs/fig-traj00144.svg")
pl(ii)
dev.off()
@

\section{Trajectory 00145}
<<traj00145,include=TRUE, fig=TRUE>>=
traj <- "traj00145"
ii <- interact(traj)
resultChi <- chisq.test(ii[2])
resultTraj00145 <-summtraj(resultChi)
resultTraj00145
pl(ii)
svg(filename = "graphs/fig-traj00145.svg")
pl(ii)
dev.off()
@

\section{Big summary}
<<results=tex>>=
traj <- sort(unique(dt$trajectory))
groupSize <- c(resultTraj00006$total,
               resultTraj00007$total,
               resultTraj00012$total,
               resultTraj00013$total,
               resultTraj00095$total,
               resultTraj00096$total,
               resultTraj00100$total,
               resultTraj00130$total,
               resultTraj00143$total,
               resultTraj00144$total,
               resultTraj00145$total
               )
favourites <- c(resultTraj00006$favourites,
                resultTraj00007$favourites,
                resultTraj00012$favourites,
                resultTraj00013$favourites,
                resultTraj00095$favourites,
                resultTraj00096$favourites,
                resultTraj00100$favourites,
                resultTraj00130$favourites,
                resultTraj00143$favourites,
                resultTraj00144$favourites,
                resultTraj00145$favourites
                )
steps <- c(resultTraj00006$totSteps,
                resultTraj00007$totSteps,
                resultTraj00012$totSteps,
                resultTraj00013$totSteps,
                resultTraj00095$totSteps,
                resultTraj00096$totSteps,
                resultTraj00100$totSteps,
                resultTraj00130$totSteps,
                resultTraj00143$totSteps,
                resultTraj00144$totSteps,
                resultTraj00145$totSteps
                )
chisq <- c(resultTraj00006$chisquare,
                resultTraj00007$chisquare,
                resultTraj00012$chisquare,
                resultTraj00013$chisquare,
                resultTraj00095$chisquare,
                resultTraj00096$chisquare,
                resultTraj00100$chisquare,
                resultTraj00130$chisquare,
                resultTraj00143$chisquare,
                resultTraj00144$chisquare,
                resultTraj00145$chisquare
                )
pval <- c(resultTraj00006$pvalue,
                resultTraj00007$pvalue,
                resultTraj00012$pvalue,
                resultTraj00013$pvalue,
                resultTraj00095$pvalue,
                resultTraj00096$pvalue,
                resultTraj00100$pvalue,
                resultTraj00130$pvalue,
                resultTraj00143$pvalue,
                resultTraj00144$pvalue,
                resultTraj00145$pvalue
                )
summdf <- data.frame(traj,groupSize,fav=favourites, steps=round(steps,0), chisq, pval)          

nestID <- read.table("nestIDtraj.csv", h=T)

summTab <- merge(nestID,summdf,by="traj")
library(xtable)
print(xtable(summTab,
             display = c("s","s","s","s","s","s","g","g")),
      math.style.exponents=TRUE
      )
write.csv(summTab, file = "summaryFavourites.csv", row.names = FALSE)
favourites/groupSize
mean(favourites/groupSize)
@

\section{Regression}
<<favouritismLine, include=TRUE, fig=TRUE>>=
m1<-glm(favourites~groupSize)
m0<-glm(favourites~offset(0+groupSize))
AIC(m0,m1)
anova(m0,m1, test="F")
pfav <- anova(m0,m1, test="F")$'Pr(>F)'[2]
library(scales)
# digits = 3 is the default but I am setting it here to be explicit,
# and draw attention to the fact this is different than the formatC
# solution.
pfav <- scientific(pfav, digits = 3)
pfav
anova(m1,test="F")
coef(m1)
plot(favourites~jitter(groupSize,factor=2)
     ,lwd = 3
     ,cex = 3
     ,pch = 21
     ,xlab = "number of termites in arena"
     ,ylab = "number of favourites"
     )
curve(coef(m1)[1]+coef(m1)[2]*x, add=T,lwd=3) 
curve(0+1*x, add = T, col = "red", lwd = 3, lty=2)
eq0 <- c('y ~ x')
eq0
text(5.7,7,paste("no favouritism:", eq0,sep='\n'),col="red", adj=1)

eq1 <- paste('y ~'
    ,round(coef(m1)[1],3)
    ,'+'
    ,round(coef(m1)[2],3)
    , 'x'
    )
eq1
pval <- paste('P = ', pfav)
text(12,4, paste(
    'favouritism:'
    ,eq1
    ,pval
    ,sep='\n'
  )
  )
@

\section*{Raw data}
<<>>=
dt
@

\end{document}